package yhao.infra.service.datasource;

import com.alibaba.druid.Constants;

import java.util.Properties;

/**
 * @author yoara
 * <p> Created on 2019/8/12 </p>
 */
public class BaseDaoSqlProperties {
    /**服务器地址**/
    private String url;

    /**服务器用户名**/
    private String username;

    /**服务器密码**/
    private String password;

    /**初始化线程池大小，默认10**/
    private int druidInitialSize = 10;

    /**最小连接数，默认10**/
    private int druidMinIdle = 10;

    /**最大连接数，默认200**/
    private int druidMaxActive = 200;

    /**获取连接时最大等待时间，单位毫秒，默认10000**/
    private int druidMaxWait = 10000;

    /**执行validationQuery检测连接是否有效，默认300000**/
    private int druidTimeBetweenEvictionRunsMillis = 300000;

    /**Destroy线程中如果检测到当前连接的最后活跃时间和当前时间的差值大于，则关闭连接。默认60000**/
    private int druidMinEvictableIdleTimeMillis = 60000;

    /**连接创建线程池执行器容量配置**/
    private int druidScheduledExecutorPoolSize = 10;

    /**是否缓存preparedStatement，也就是PSCache。PSCache对支持游标的数据库性能提升巨大，比如说oracle。在mysql下建议关闭。**/
    private boolean druidPoolPreparedStatements = false;

    /**sql统计**/
    private StatFilterProperties druidStat = new StatFilterProperties();

    /**连接泄露检测**/
    private Abandoned abandoned = new Abandoned();

    public static class Abandoned{
        /**打开removeAbandoned功能**/
        private boolean enable = false;

        /**连接未关闭超时时间**/
        private int removeAbandonedTimeout = 1800;

        /**输出堆栈日志**/
        private boolean logAbandoned = true;

        public boolean isEnable() {
            return enable;
        }

        public void setEnable(boolean enable) {
            this.enable = enable;
        }

        public int getRemoveAbandonedTimeout() {
            return removeAbandonedTimeout;
        }

        public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
            this.removeAbandonedTimeout = removeAbandonedTimeout;
        }

        public boolean isLogAbandoned() {
            return logAbandoned;
        }

        public void setLogAbandoned(boolean logAbandoned) {
            this.logAbandoned = logAbandoned;
        }
    }

    public static class StatFilterProperties{
        /**是否开启，默认不开启**/
        private boolean enable = false;

        /**统计拦截器容量，默认3000**/
        private int druidStatSqlMaxSize = 3000;

        /**statFilter合并统计**/
        private boolean druidStatMergeSql = true;

        /**statFilter统计慢查询**/
        private boolean druidStatLogSlowSql = true;

        /**statFilter慢查询时长定义**/
        private int druidStatSlowSqlMillis = 5000;

        public boolean isEnable() {
            return enable;
        }

        public void setEnable(boolean enable) {
            this.enable = enable;
        }

        public int getDruidStatSqlMaxSize() {
            return druidStatSqlMaxSize;
        }

        public void setDruidStatSqlMaxSize(int druidStatSqlMaxSize) {
            this.druidStatSqlMaxSize = druidStatSqlMaxSize;
        }

        public boolean isDruidStatMergeSql() {
            return druidStatMergeSql;
        }

        public void setDruidStatMergeSql(boolean druidStatMergeSql) {
            this.druidStatMergeSql = druidStatMergeSql;
        }

        public boolean isDruidStatLogSlowSql() {
            return druidStatLogSlowSql;
        }

        public void setDruidStatLogSlowSql(boolean druidStatLogSlowSql) {
            this.druidStatLogSlowSql = druidStatLogSlowSql;
        }

        public int getDruidStatSlowSqlMillis() {
            return druidStatSlowSqlMillis;
        }

        public void setDruidStatSlowSqlMillis(int druidStatSlowSqlMillis) {
            this.druidStatSlowSqlMillis = druidStatSlowSqlMillis;
        }
    }

    public Abandoned getAbandoned() {
        return abandoned;
    }

    public void setAbandoned(Abandoned abandoned) {
        this.abandoned = abandoned;
    }

    public StatFilterProperties getDruidStat() {
        return druidStat;
    }

    public void setDruidStat(StatFilterProperties druidStat) {
        this.druidStat = druidStat;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getDruidInitialSize() {
        return druidInitialSize;
    }

    public void setDruidInitialSize(int druidInitialSize) {
        this.druidInitialSize = druidInitialSize;
    }

    public int getDruidMinIdle() {
        return druidMinIdle;
    }

    public void setDruidMinIdle(int druidMinIdle) {
        this.druidMinIdle = druidMinIdle;
    }

    public int getDruidMaxActive() {
        return druidMaxActive;
    }

    public void setDruidMaxActive(int druidMaxActive) {
        this.druidMaxActive = druidMaxActive;
    }

    public int getDruidMaxWait() {
        return druidMaxWait;
    }

    public void setDruidMaxWait(int druidMaxWait) {
        this.druidMaxWait = druidMaxWait;
    }

    public int getDruidTimeBetweenEvictionRunsMillis() {
        return druidTimeBetweenEvictionRunsMillis;
    }

    public void setDruidTimeBetweenEvictionRunsMillis(int druidTimeBetweenEvictionRunsMillis) {
        this.druidTimeBetweenEvictionRunsMillis = druidTimeBetweenEvictionRunsMillis;
    }

    public int getDruidMinEvictableIdleTimeMillis() {
        return druidMinEvictableIdleTimeMillis;
    }

    public void setDruidMinEvictableIdleTimeMillis(int druidMinEvictableIdleTimeMillis) {
        this.druidMinEvictableIdleTimeMillis = druidMinEvictableIdleTimeMillis;
    }

    public int getDruidScheduledExecutorPoolSize() {
        return druidScheduledExecutorPoolSize;
    }

    public void setDruidScheduledExecutorPoolSize(int druidScheduledExecutorPoolSize) {
        this.druidScheduledExecutorPoolSize = druidScheduledExecutorPoolSize;
    }

    public boolean isDruidPoolPreparedStatements() {
        return druidPoolPreparedStatements;
    }

    public void setDruidPoolPreparedStatements(boolean druidPoolPreparedStatements) {
        this.druidPoolPreparedStatements = druidPoolPreparedStatements;
    }

    public Properties getAdditionalProperties(){
        Properties prop = new Properties();
        prop.put(Constants.DRUID_STAT_SQL_MAX_SIZE,druidStat.getDruidStatSqlMaxSize()+"");
        return prop;
    }
}
